from po_model.Unit1.base import base
from selenium.webdriver.common.by import By
from time import sleep

class page(base):
    denglu_loc= (By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[1]/a')
    username_loc= (By.NAME,'username')
    password_loc= (By.XPATH,'//*[@id="theForm"]/div[2]/span[2]/input')
    denglubtn_loc= (By.XPATH,'//*[@id="theForm"]/div[5]/a')
    shouye_loc= (By.XPATH,'/html/body/div[3]/div[2]/ul/li[1]/a')
    select_loc= (By.XPATH,'//*[@id="searchForm"]/div[1]/input')
    selectbtn_loc= (By.XPATH,'//*[@id="searchForm"]/div[2]/input')
    spxiangqing_loc= (By.XPATH,'/html/body/div[5]/div[2]/div[2]/div[1]/ul/li/div[1]/span/a/img')
    buy_loc= (By.ID,'buy-cart')
    address_loc= (By.ID,'address-61')
    next_loc= (By.ID,'order-button')
    look_loc= (By.XPATH,'/html/body/div[4]/div[3]/p/a')
    two_info_loc= (By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[1]/a')
    wode_dd_loc= (By.XPATH,'/html/body/div[5]/div[1]/div[2]/div[1]/div[2]/ul/li[1]/a')
    dd_xiang_loc= (By.XPATH,'/html/body/div[5]/div[2]/div[1]/div[2]/table/tbody/tr[3]/td[3]/div[2]/a')

    def denglu_shouye(self):
        self.click(self.denglu_loc)

    def username_text(self):
        self.send_keys(self.username_loc,'18238680385')

    def password_text(self):
        self.send_keys(self.password_loc,'123456')

    def denglu_btn(self):
        self.click(self.denglubtn_loc)

    def denglu_duanyan(self):
        denglu = self.driver.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/ul/li[1]/a').text
        print(denglu)
        denglu1 = denglu.replace("欢迎回来，", "")
        print(denglu1)
        # 查询数据库名称
        username = self.sql.select("select username from mall_users where id='330'")
        print(username[0][0])
        # 登录用户信息数据库断言
        try:
            assert denglu1, username[0][0]
            print("用户名断言成功")
        except Exception as e:
            print(f"断言失败:{e}")

    qian_yue=None
    def login_money(self):
        # 支付前余额
        page.qian_yue = self.sql.select(f"select amount from mall_users where id='330'")
        print(f'支付前的余额：{page.qian_yue[0][0]}')


    def shouye_click(self):
        self.click(self.shouye_loc)

    def select_sendkeys(self):
        self.send_keys(self.select_loc,'桌面取暖器')

    def select_btn(self):
        self.click(self.selectbtn_loc)

    def sp_xiangqing(self):
        self.click(self.spxiangqing_loc)

    # 商品价格
    spprice=None
    def sp_price(self):
        page.spprice= self.driver.find_element(By.ID,'sell-price-box').text
        print(f"商品价格：{page.spprice}")

    def buy(self):
        self.click(self.buy_loc)

    def address(self):
        self.click(self.address_loc)

    # 滑动
    def hua(self):
        self.huadong()

    def next(self):
        self.click(self.next_loc)

    def look(self):
        self.click(self.look_loc)

    def two_info(self):
        self.click(self.two_info_loc)

    # 支付后查看余额
    afteryue1=None
    def after_yue(self):
        afteryue=self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li[4]/span').text
        page.afteryue1=afteryue.replace("¥ ",'')
        print(f"支付后的余额：{page.afteryue1}")

        # 支付前的余额 - 商品价格 = 支付后的余额
        total_price=float(page.qian_yue[0][0])-float(page.spprice)
        print(total_price)

        assert float(total_price),float(page.afteryue1)

    def wode_dd(self):
        self.click(self.wode_dd_loc)

    def dd_xiang(self):
        self.click(self.dd_xiang_loc)



    # def luoji_A3mall(self):
    #     self.get_url("http://10.59.9.12:7001/")
    #     sleep(2)
    #     self.denglu_shouye()
    #     sleep(1)
    #     self.username_text()
    #     sleep(1)
    #     self.password_text()
    #     sleep(6)
    #     self.denglu_btn()
    #     sleep(5)
    #     # 数据库用户信息断言
    #     self.denglu_duanyan()
    #     sleep(1)
    #     # 节点断言登录时的余额
    #     self.login_money()
    #
    #     sleep(1)
    #     self.shouye_click()
    #     sleep(1)
    #     self.select_sendkeys()
    #     sleep(1)
    #     self.select_btn()
    #     sleep(1)
    #     self.sp_xiangqing()
    #     sleep(2)
    #     # 节点断言商品价格
    #     self.sp_price()
    #     sleep(1)
    #     self.buy()
    #     sleep(1)
    #     self.address()
    #     sleep(1)
    #     # 滑动
    #     self.hua()
    #     sleep(1)
    #     self.next()
    #     sleep(1)
    #     self.look()
    #     sleep(1)
    #     self.two_info()
    #     # 支付成功后的余额
    #     self.after_yue()
    #     sleep(1)
    #     self.wode_dd()
    #     sleep(1)
    #     self.dd_xiang()
